import logging
import datetime

from .protocol import RequestHeaders, RequestBody, TIME_FMT


class BaseHandle:

    def __init__(self):
        self.resolve()

    def resolve(self):
        self.resolve_headers()
        self.resolve_body()

    def resolve_headers(self):
        raise NotImplementedError

    def resolve_body(self):
        raise NotImplementedError


class Request(BaseHandle):

    def __init__(self, data: dict, sock, key, mask):
        self.data = data
        self.sock = sock
        self.key = key
        self.mask = mask
        self.headers = None
        self.body = None

        super().__init__()

    def resolve_headers(self):
        try:
            h: dict = self.data["headers"]
            method = h.get("method")
            accept = h.get("accept")
            # accept_lang = h.get("accept_lang")
            token = h.get("token")
            date = datetime.datetime.now().strftime(TIME_FMT)
            version = h.get("version")

            self.headers = RequestHeaders(method, accept, token, date, version)

        except KeyError as e:
            logging.error(e)
        except TypeError as e:
            print(e)
            print("#" * 100)
            print(self.data)
            print("#" * 100)

    def resolve_body(self):
        try:
            body: dict = self.data["body"]
            account = body.get("account")
            password = body.get("password")
            number = body.get("number")
            pic = body.get("pic")
            info = body.get("info")
            img = body.get("img")
            msg = body.get("msg")
            name = body.get("name")
            self.body = RequestBody(account, password, number, pic, info, img, msg, name)

        except KeyError as e:
            logging.error(e)


class Response(BaseHandle):
    def __init__(self):
        self.status = None
        self.location = None
        self.content_type = None
        self.content_length = None
        self.connection = None
        self.body = None
        super().__init__()

    def resolve_headers(self):
        pass

    def resolve_body(self):
        pass

    def __repr__(self):
        pass

    __str__ = __repr__
